﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Common;
using DLMemberPointMS.Entity.ViewModel;
namespace DLMemberPointMS.Business
{
    public   class ConsumeOrdersService : BaseService<ConsumeOrders>
    {


        #region 减积分
        /// <summary>
        /// 减积分
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult GetJianJifen(JianJiFenMemberViewModel viewModel)
        {
            var query = PredicateExtensions.True<ConsumeOrders>();


            //添加条件
            //手机号/卡号
            if (!string.IsNullOrEmpty(viewModel.MC_CardID))
            {
                if (viewModel.MC_CardID.Length == 11)
                {
                    query = query.And(e => e.MemCards.MC_Mobile == viewModel.MC_CardID);
                }
                else
                {
                    query = query.And(e => e.MC_CardID.Contains(viewModel.MC_CardID));
                }
            }
            //消费日期
            if (viewModel.StartTime.ToString() != "0001/1/1 0:00:00" && viewModel.EndTime.ToString() != "0001/1/1 0:00:00")
            {
                query = query.And(e => e.CO_CreateTime >= viewModel.StartTime && e.CO_CreateTime <= viewModel.EndTime);

            }
            //会员等级
            if (!string.IsNullOrEmpty(viewModel.CL_ID.ToString()) && viewModel.CL_ID > 0)
            {
                query = query.And(e => e.MemCards.CL_ID == viewModel.CL_ID);
            }

            //减去积分
            //获取下拉列表的值

            if (!string.IsNullOrEmpty(viewModel.CO_GavePoint.ToString()) && viewModel.CO_GavePoint > 0)
            {

                if (viewModel.Minus == ">=")
                {
                    query = query.And(e => e.CO_GavePoint >= viewModel.CO_GavePoint);
                }
                if (viewModel.Minus == "<=")
                {
                    query = query.And(e => e.CO_GavePoint <= viewModel.CO_GavePoint);
                }
                if (viewModel.Minus == "<")
                {
                    query = query.And(e => e.CO_GavePoint < viewModel.CO_GavePoint);
                }
                if (viewModel.Minus == "=")
                {
                    query = query.And(e => e.CO_GavePoint == viewModel.CO_GavePoint);
                }
                if (viewModel.Minus == ">")
                {
                    query = query.And(e => e.CO_GavePoint > viewModel.CO_GavePoint);
                }
            }
            //订单编号
            if (!string.IsNullOrEmpty(viewModel.CO_OrderCode))
            {
                query = query.And(e => e.CO_OrderCode.Contains(viewModel.CO_OrderCode));
            }
            query = query.And(e => (orderTypeEnum)e.CO_OrderType == orderTypeEnum.减积分);
            int rowCount = 0;//总条数
            MemCardsService mc = new MemCardsService();
            var pageData = GetList(viewModel.page, viewModel.limit, ref rowCount, query, e => e.CO_ID, true).Select(
                e => new
                {
                    //name = mc.GetMC_Name(e.MC_CardID),
                    e.CO_OrderCode,
                    e.CO_GavePoint,
                    e.CO_CreateTime,
                    e.CO_Remark,
                    e.MC_CardID,
                    e.MemCards.MC_Name,
                    e.MemCards.MC_Mobile,
                    e.MemCards.CL_ID,
                    e.CO_Recash

                }).ToList();
            //pageData.name=mc.Find(pageData.MC_CardID)
            return new OperateResult(code.Success, "", pageData, rowCount);
        }
#endregion
      #region 积分返现
        /// <summary>
        /// 积分返现
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult GetJiFenFanXian(JianJiFenMemberViewModel viewModel)
        {
            var query = PredicateExtensions.True<ConsumeOrders>();


            //添加条件
            //手机号/卡号
            if (!string.IsNullOrEmpty(viewModel.MC_CardID))
            {
                if (viewModel.MC_CardID.Length == 11)
                {
                    query = query.And(e => e.MemCards.MC_Mobile == viewModel.MC_CardID);
                }
                else
                {
                    query = query.And(e => e.MC_CardID.Contains(viewModel.MC_CardID));
                }
            }
            //消费日期
            if (viewModel.StartTime.ToString() != "0001/1/1 0:00:00" && viewModel.EndTime.ToString() != "0001/1/1 0:00:00")
            {
                query = query.And(e => e.CO_CreateTime >= viewModel.StartTime && e.CO_CreateTime <= viewModel.EndTime);

            }
            //会员等级
            if (!string.IsNullOrEmpty(viewModel.CL_ID.ToString()) && viewModel.CL_ID > 0)
            {
                query = query.And(e => e.MemCards.CL_ID == viewModel.CL_ID);
            }

            //减去积分
            //获取下拉列表的值

            if (!string.IsNullOrEmpty(viewModel.MC_TotalMoney.ToString()) && viewModel.MC_TotalMoney > 0)
            {

                if (viewModel.Minus == ">=")
                {
                    query = query.And(e => e.MemCards.MC_TotalMoney >= viewModel.MC_TotalMoney);
                }
                if (viewModel.Minus == "<=")
                {
                    query = query.And(e => e.MemCards.MC_TotalMoney <= viewModel.MC_TotalMoney);
                }
                if (viewModel.Minus == "<")
                {
                    query = query.And(e => e.MemCards.MC_TotalMoney < viewModel.MC_TotalMoney);
                }
                if (viewModel.Minus == "=")
                {
                    query = query.And(e => e.MemCards.MC_TotalMoney == viewModel.MC_TotalMoney);
                }
                if (viewModel.Minus == ">")
                {
                    query = query.And(e => e.MemCards.MC_TotalMoney > viewModel.MC_TotalMoney);
                }
            }
            //订单编号
            if (!string.IsNullOrEmpty(viewModel.CO_OrderCode))
            {
                query = query.And(e => e.CO_OrderCode.Contains(viewModel.CO_OrderCode));
            }
            query = query.And(e => (orderTypeEnum)e.CO_OrderType == orderTypeEnum.兑换返现);
            int rowCount = 0;//总条数
            MemCardsService mc = new MemCardsService();
            var pageData = GetList(viewModel.page, viewModel.limit, ref rowCount, query, e => e.CO_ID, true).Select(
                e => new
                {
                    //name = mc.GetMC_Name(e.MC_CardID),
                    e.CO_OrderCode,
                    e.CO_GavePoint,
                    e.CO_CreateTime,
                    e.CO_Remark,
                    e.MC_CardID,
                    e.MemCards.MC_Name,
                    e.MemCards.MC_Mobile,
                    e.MemCards.CL_ID,
                    e.CO_Recash

                }).ToList();
            //pageData.name=mc.Find(pageData.MC_CardID)
            return new OperateResult(code.Success, "", pageData, rowCount);
        }
        #endregion
        #region 快速消费

        public OperateResult GetFastJifen(JianJiFenMemberViewModel viewModel)
        {
            var query = PredicateExtensions.True<ConsumeOrders>();


            //添加条件
            //手机号/卡号
            if (!string.IsNullOrEmpty(viewModel.MC_CardID))
            {
                if (viewModel.MC_CardID.Length == 11)
                {
                    query = query.And(e => e.MemCards.MC_Mobile == viewModel.MC_CardID);
                }
                else
                {
                    query = query.And(e => e.MC_CardID.Contains(viewModel.MC_CardID));
                }
            }
            //消费日期
            if (viewModel.StartTime.ToString() != "0001/1/1 0:00:00" && viewModel.EndTime.ToString() != "0001/1/1 0:00:00")
            {
                query = query.And(e => e.CO_CreateTime >= viewModel.StartTime && e.CO_CreateTime <= viewModel.EndTime);

            }
            //会员等级
            if (!string.IsNullOrEmpty(viewModel.CL_ID.ToString()) && viewModel.CL_ID > 0)
            {
                query = query.And(e => e.MemCards.CL_ID == viewModel.CL_ID);
            }

            //减去积分
            //获取下拉列表的值

            if (!string.IsNullOrEmpty(viewModel.CO_DiscountMoney.ToString()) && viewModel.CO_DiscountMoney > 0)
            {

                if (viewModel.Minus == ">=")
                {
                    query = query.And(e => e.CO_DiscountMoney >= viewModel.CO_DiscountMoney);
                }
                if (viewModel.Minus == "<=")
                {
                    query = query.And(e => e.CO_DiscountMoney <= viewModel.CO_DiscountMoney);
                }
                if (viewModel.Minus == "<")
                {
                    query = query.And(e => e.CO_DiscountMoney < viewModel.CO_DiscountMoney);
                }
                if (viewModel.Minus == "=")
                {
                    query = query.And(e => e.CO_DiscountMoney == viewModel.CO_DiscountMoney);
                }
                if (viewModel.Minus == ">")
                {
                    query = query.And(e => e.CO_DiscountMoney > viewModel.CO_DiscountMoney);
                }
            }
            //订单编号
            if (!string.IsNullOrEmpty(viewModel.CO_OrderCode))
            {
                query = query.And(e => e.CO_OrderCode.Contains(viewModel.CO_OrderCode));
            }
            query = query.And(e => (orderTypeEnum)e.CO_OrderType == orderTypeEnum.快速消费);
            int rowCount = 0;//总条数
            MemCardsService mc = new MemCardsService();
            var pageData = GetList(viewModel.page, viewModel.limit, ref rowCount, query, e => e.CO_ID, true).Select(
                e => new
                {
                    //name = mc.GetMC_Name(e.MC_CardID),
                    e.CO_OrderCode,
                    e.CO_GavePoint,
                    e.CO_CreateTime,
                    e.CO_Remark,
                    e.MC_CardID,
                    e.MemCards.MC_Name,
                    e.MemCards.MC_Mobile,
                    e.MemCards.CL_ID,
                    e.CO_Recash,
                    e.CO_DiscountMoney

                }).ToList();
            //pageData.name=mc.Find(pageData.MC_CardID)
            return new OperateResult(code.Success, "", pageData, rowCount);
        }
        #endregion
        #region 会员消费统计
       public OperateResult GetMemConsun(JianJiFenMemberViewModel viewModel)
        {
            var query = PredicateExtensions.True<ConsumeOrders>();
            //判断是否有条件 消费日期
            if (viewModel.StartTime.ToString() != "0001/1/1 0:00:00" && viewModel.EndTime.ToString() != "0001/1/1 0:00:00")
            {
                query = query.And(e => e.CO_CreateTime >= viewModel.StartTime && e.CO_CreateTime <= viewModel.EndTime);

            }
            if (!string.IsNullOrWhiteSpace(viewModel.MC_CardID))
            {
                //卡号手机号
                if (viewModel.MC_CardID.Length > 6)
                {
                    query = query.And(e => e.MemCards.MC_Mobile == viewModel.MC_CardID);
                }
                else
                {
                    query = query.And(e => e.MC_CardID.Contains(viewModel.MC_CardID));
                }
            }
        
            //消费类型
            if (viewModel.CO_OrderType.ToString()!="" && viewModel.CO_OrderType > 0){
                query = query.And(e => e.CO_OrderType == viewModel.CO_OrderType);
            }
            int rowCount = 0;

            var pageData = GetList(viewModel.page, viewModel.limit, ref rowCount, query, e => e.CO_ID, true).Select(e => new
            {

                e.MemCards.MC_CardID,
                e.MemCards.MC_Name,
                e.MemCards.MC_TotalMoney,
                //name = mc.GetMC_Name(e.MC_CardID),
                e.CO_OrderCode,
                e.CO_GavePoint,
                e.CO_CreateTime,
                e.CO_Remark,
                CO_OrderType=((orderTypeEnum)e.CO_OrderType).ToString(),
                e.MemCards.MC_Mobile,
                e.MemCards.CL_ID,
                e.CO_Recash,
                e.CO_DiscountMoney
            }).ToList();

            return new OperateResult(code.Success, "", pageData,rowCount);
  

        }
        #endregion
    }
}
